package com.venue.hjt.venueclass.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Handler
import android.util.Log
import android.view.View
import com.bumptech.glide.Glide
import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.HomeAdapter
import com.venue.hjt.venueclass.adapter.RecordAdapter
import com.venue.hjt.venueclass.bean.API
import com.venue.hjt.venueclass.db.CardDao
import com.venue.hjt.venueclass.db.VenueHour
import com.venue.hjt.venueclass.model.HomeModel
import com.venue.hjt.venueclass.utlis.*
import com.youth.banner.Banner
import com.youth.banner.Transformer
import kotlinx.android.synthetic.main.fragment_home.*
import okhttp3.Call
import java.lang.Exception
import java.util.*

/**
 * Created by xqx on 2018/3/6.
 *
 */
class TabHomeFragment :BaseFragment(){
    override fun initView(view: View?) {

    }
    override fun getLayoutResource(): Int {
        return R.layout.fragment_home
    }
    var model: HomeModel?=null
    var homeReceiver:HomeReceiver?=null
    val list=ArrayList<VenueHour>()
    var adapter:HomeAdapter?=null
    override fun loadData() {

        homeReceiver=HomeReceiver()
        model= HomeModel()
        activity.registerReceiver(homeReceiver,filter())
        banner!!.setImageLoader(GlideImageLoader())
        banner!!.setImages(model!!.album)
        banner!!.setBannerAnimation(Transformer.CubeOut)
        banner!!.setDelayTime(6000)
        banner!!.start()
        val dialog=DialogUtlis()
        ll_order.setOnClickListener({
            dialog.showDialog(activity,0,"刷卡后马上进行场馆预约","","","","","","'","","",0,0)

        })
        ll_complaint.setOnClickListener({
            dialog.showDialog(activity,1,"刷卡后马上进行投诉","","","","","","","","",0,0)
        })
        tv_home_time.text=ClassCalendar.getStringDay1()
        tv_home_content.text=model!!.complaint
        tv_home_name.text=model!!.className()
        tv_home_num.text=model!!.num
        tv_home_dynamic.text=model!!.dynamic
        starBar.setTouch(false)
        starBar.starMark = model!!.score

        displayTv.text=model!!.score.toString()
        artSchool()
        marqueeView.startWithList(artSchool)
        Log.e("ATG",ClassCalendar.getWeekDays1().toString()+" 周")
        list+=CardDao.queryHourWeek(ClassCalendar.getWeekDays1().toString())
        adapter= HomeAdapter(activity,list)
        gv_home_time.adapter=adapter
        tv_home_venue_name.text=model!!.className()
        val timer = Timer()
        var isTime=true
        var group_id=""
//        var isComplate=true
        timer.schedule(object : TimerTask() {
            override fun run() {
               activity.runOnUiThread({
                  val time= ClassCalendar.getIntDayVenue()
                   val mTime = model!!.time
                   val str=APPUtlis.getTimeExpend(time,ClassCalendar.getIntDayVenue1()+" "+mTime.start_time)
                   val list=str.split(":")
                   if(mTime.end_time=="0"){
                       tv_home_y_time.text="当天已无预约"
                       ll_home_no.visibility=View.GONE
                       tv_home_q.visibility=View.GONE
                       tv_venue_to.visibility=View.GONE
                       tv_venue_no.visibility=View.GONE
//                       Log.e("DATA",isTime.toString())
                       if (!isTime){
                           Log.e("DATA",API.VENUEID+Constant.SNO+"/"+group_id)
                            OkhttPost.Okget(API.VENUEID+Constant.SNO+"/"+group_id,1,object :OkhttPost.Callback{
                                override fun onError(call: Call?, e: Exception?, id: Int) {
                                    isTime=true
//                                    Log.e("DATA",e!!.message)
                                }
                                override fun onResponse(response: String?, id: Int) {
                                    isTime=true
//                                    Log.e("DATA",response)
                                }
                            })
                       }
                   }else{
                       tv_home_y_time.text=mTime.start_time+"～"+mTime.end_time+"预约"
                       val parseInt = Integer.parseInt(list[0])
                       val parseInt1 = Integer.parseInt(list[1])
                       if (parseInt>=0&&parseInt1>=1){
                           ll_home_no.visibility=View.VISIBLE
                           tv_home_q.visibility=View.GONE
                           tv_venue_to.visibility=View.VISIBLE
                           tv_venue_no.visibility=View.VISIBLE
                           tv_home_hour.text=list[0]
                           tv_home_minute.text=list[1]
//                           Log.e("DATA",isTime.toString())
                           if (!isTime){
//                               Log.e("DATA",API.VENUEID+Constant.SNO+"/"+group_id)
                               OkhttPost.Okget(API.VENUEID+Constant.SNO+"/"+group_id,1,object :OkhttPost.Callback{
                                   override fun onError(call: Call?, e: Exception?, id: Int) {
                                       isTime=true
//                                       Log.e("DATA",e!!.message)
                                   }
                                   override fun onResponse(response: String?, id: Int) {
                                       isTime=true
//                                       Log.e("DATA",response)
                                   }
                               })
                           }
                       }else{
//                           Log.e("DATA",isTime.toString())
                           isTime=false
                           val groups = mTime.groups
                           if (groups.size!=0){
                               group_id= groups[0].booking_id
                           }
                           ll_home_no.visibility=View.GONE
                           tv_home_q.visibility=View.VISIBLE
                           tv_venue_to.visibility=View.VISIBLE
                           tv_venue_no.visibility=View.VISIBLE
                       }
                       val groupId = CardDao.queryVenueGroupId(mTime.id, "1")
                       Log.e("ATG",mTime.id)
                       tv_venue_to.text="已到 "+groupId.size.toString()+"人"
                       val group = CardDao.queryVenueGroupId(mTime.id, "0")
                       tv_venue_no.text="未到 "+group.size.toString()+" 人"
                   }
               })
            }
        }, 10000, 8000)

    }
    /**
     * 广播类
     */
    inner class HomeReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            when (action) {
                Constant.ALBUM->{
                    banner!!.update(model!!.album)
                }
                Constant.VENUEHOUR->{
                    Log.e("ATG","123fdf")
                    ScheduleWeek()
                }
                Constant.ARTSCHOOL->{
                    artSchool()
                    marqueeView!!.startWithList(artSchool)
                }
                Constant.HOMETEXT->{
                    tv_home_venue_name.text=model!!.className()
                    tv_home_content.text=model!!.complaint
                    tv_home_name.text=model!!.className()
                    tv_home_num.text=model!!.num
                    tv_home_dynamic.text=model!!.dynamic
                    starBar.starMark = model!!.score
                    displayTv.text=model!!.score.toString()
                    list.clear()
                    list+=CardDao.queryHourWeek(ClassCalendar.getWeekDays1().toString())
                    if (adapter==null){
                        adapter= HomeAdapter(activity,list)
                    }else{
                        adapter!!.notifyDataSetChanged()
                    }

                }
            }
        }
    }
    fun ScheduleWeek(){
        list.clear()
        list += CardDao.queryHourWeek(ClassCalendar.getWeekDays().toString())
        adapter!!.notifyDataSetChanged()
    }
    /**
     * 校园动态
     */
    val artSchool=ArrayList<String>()
    fun artSchool() {
        artSchool.clear()
        val artClass = model!!.ArtSchools()
        artClass.mapTo(artSchool) { it.trim() }
    }
    /**
     * @return IntentFilter 广播
     */
    private fun filter(): IntentFilter {
        val filter = IntentFilter()
        filter.addAction(Constant.WEATHER)
        filter.addAction(Constant.INFO)
        filter.addAction(Constant.ALBUM)
        filter.addAction(Constant.ARTSCHOOL)
        filter.addAction(Constant.LABOUR)
        filter.addAction(Constant.HOMETEXT)
        filter.addAction(Constant.VENUEHOUR)
        return filter
    }
}